import Vue from 'vue'
import store from './store'
import App from './App'
import './static/font/iconfont.css'

const msg = (title, duration=1500, mask=false, icon='none')=>{
	//统一提示方便全局修改
	if(Boolean(title) === false){
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
const json = type=>{
	//模拟异步请求数据
	return new Promise(resolve=>{
		setTimeout(()=>{
			resolve(Json[type]);
		}, 500)
	})
}

const prePage = ()=>{
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}

const postUrl = function(url, data, fun){
	uni.request({
		url:url,
		method:"POST",
		dataType:"JSON",
		header:{'content-type':'application/json'},
		data:data,
		success:function(res){
			let _data = {};
			if(typeof(res.data) == "object"){
				_data = res.data;
			}else{
				_data = JSON.parse(res.data);
			}
			
			// token失效
			if(_data.code == 4001){
				uni.redirectTo({
					url: '/pages/public/login'
				})
			}
			
			if(_data.code != 200){
				uni.showToast({
					title: _data.msg
				});
			}else{
				fun(_data.data)
			}
		},
		fail:function(){
			uni.showToast({
				title: "接口出现错误"
			});
		}
	})
}

const getUrl = function(url, data, fun){
	uni.request({
		url:url,
		method:"GET",
		dataType:"JSON",
		header:{'content-type':'application/json'},
		data:data,
		success:function(res){
			let _data = {};
			if(typeof(res.data) == "object"){
				_data = res.data;
			}else{
				_data = JSON.parse(res.data);
			}
			
			// token失效
			if(_data.code == 4001){
				uni.redirectTo({
					url: '/pages/public/login'
				})
			}
			
			if(_data.code != 200){
				uni.showToast({
					title: _data.msg
				});
			}else{
				fun(_data.data)
			}
		},
		fail:function(){
			uni.showToast({
				title: "接口出现错误"
			});
		}
	})
}

const putUrl = function(url, data, fun){
	uni.request({
		url:url,
		method:"PUT",
		dataType:"JSON",
		header:{'content-type':'application/json'},
		data:data,
		success:function(res){
			this.$api.loading(false)
			let _data = {};
			if(typeof(res.data) == "object"){
				_data = res.data;
			}else{
				_data = JSON.parse(res.data);
			}
			
			// token失效
			if(_data.code == 4001){
				uni.redirectTo({
					url: '/pages/public/login'
				})
			}
			
			if(_data.code != 200){
				uni.showToast({
					title: _data.msg
				});
			}else{
				fun(_data.data)
			}
		},
		fail:function(){
			this.$api.loading(false)
			uni.showToast({
				title: "接口出现错误"
			});
		}
	})
}

const deleteUrl = function(url, data, fun){
	uni.request({
		url:url,
		method:"DELETE",
		dataType:"JSON",
		header:{'content-type':'application/json'},
		data:data,
		success:function(res){
			this.$api.loading(false)
			let _data = {};
			if(typeof(res.data) == "object"){
				_data = res.data;
			}else{
				_data = JSON.parse(res.data);
			}
			
			// token失效
			if(_data.code == 4001){
				uni.redirectTo({
					url: '/pages/public/login'
				})
			}
			
			if(_data.code != 200){
				uni.showToast({
					title: _data.msg
				});
			}else{
				fun(_data.data)
			}
		},
		fail:function(){
			this.$api.loading(false)
			uni.showToast({
				title: "接口出现错误"
			});
		}
	})
}

const timestampToTime  = function(timestamp) {
   var date = new Date(timestamp)
   var Y = date.getFullYear() + '-'
   var M = (date.getMonth() + 1) + '-'
   var D = date.getDate() + ' '
   var h = date.getHours() + ':'
   var m = date.getMinutes() + ':'
   var s = date.getSeconds()
   return Y + M + D + h + m + s
}



const checkEmail = function (email){
	return RegExp(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/).test(email);
}

//检测手机号码格式
const checkMobile = Date.checkMobile || function (number) {  
	var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
	if (!phoneReg.test(number)) {
		return false;
	}
	else{
		return true;
	}
};

const loading = (statu, title = '加载中') => {
	// Loading
	if (Boolean(statu) === false) {
		uni.hideLoading();
	} else {
		uni.showLoading({
			title
		});
	}
}

const random_char = function(n){
	 var random_charset = [
	 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
	 	, 'a', 'b', 'c', 'd', 'e', 'f', 'g'
	 	, 'h', 'i', 'j', 'k', 'l', 'm', 'n'
	 	, 'o', 'p', 'q', 'r', 's', 't'
	 	, 'u', 'v', 'w', 'x', 'y', 'z'];
  var res = "";
  for(var i = 0; i < n; i++) {
      var id = Math.ceil(Math.random() * 35);
      res += random_charset[id];
  }
  return res;
 }
 
 const setCartNumber = function(){
	 // 显示购物车得角标
	 let _text = 0;
	 try{
	 	let userInfo = uni.getStorageSync("userInfo");
	 	if(userInfo){
	 		_text = parseInt(userInfo.cart_number)
	 		_text = isNaN(_text)? 0 : _text;
	 	}
	 }catch(e){
	 	//TODO handle the exception
	 }
	 
	 let _index = 2
	 // #ifdef APP-PLUS
	 _index = 3
	 // #endif
	 // #ifdef MP-WEIXIN
	 //_index = 3
	 // #endif
	 uni.setTabBarBadge({
	 	index: _index,
	 	text:  String(_text)
	 })
 }

// 定义全局API
//const api_url= 'https://wx.51job.top/app/'
const api_url= 'http://127.0.0.1:5566/app/'
const upload_url= 'https://oss.51job.top/'
const _api = new Array;
_api.ApiHost = api_url;
_api.ApiUpImg = api_url + "up/index";//获取直播分类
_api.ApiUpUpImg = upload_url + "upload/image";    
_api.ApiUpUpImg1 = api_url + "up/up-img1";     
_api.ApiUpDelImg = api_url + "up/del-img";          
_api.ApiSmsCode = api_url + "sms/send-code";
_api.ApiGoodsCate = api_url + "index/index/category";
_api.ApiGoodsList = api_url + "goods/list";
//_api.ApiGoodsAdd = api_url + "goods/add-goods";
//_api.ApiGoodsEdit = api_url + "goods/edit-goods";
_api.ApiGoodsGetGoods = api_url + "goods/get-goods";
_api.ApiGoodsGoodsCate = api_url + "goods/goods-cate";
_api.ApiIndexPush = api_url + "index/push";
_api.ApiIndexCoupon = api_url + "index/coupon";//领券中心 get--列表  post -- 添加

/***************** 首页 *****************/
_api.ApiIndexIndex = api_url + "index/index";
_api.ApiIndexHotSearch = api_url + "index/index/hotSearch";

/***************** 文章 *****************/
_api.ApiArticleList = api_url + "article/list";
_api.ApiArticleDetail = api_url + "article/detail";
_api.ApiAnounceDetail = api_url + "index/index/notifyDetail";
_api.ApiArticleCheck = api_url + "article/checks";

/***************** 购物车 *****************/
_api.ApiCartAdd = api_url + "cart/add";
_api.ApiCartIndex = api_url + "cart/index";
_api.ApiCartDel = api_url + "cart/del";
_api.ApiCartClear = api_url + "cart/clear";
_api.ApiCartEditer = api_url + "cart/editNumber";

/***************** 商品详情页 *****************/
_api.ApiGoodsDetail = api_url + "goods/detail";
_api.ApiGoodsCollect = api_url + "goods/collect";
_api.ApiOrderInitBuy = api_url + "order/initBuy";

/**************** 提交结算页面 ***************/
_api.ApiCartSub = api_url + "order/pre";
_api.ApiOrderCreate = api_url + "order/create";
_api.ApiOrderInfo = api_url + "order/info";
_api.ApiOrderBuy = api_url + "order/buy";
_api.ApiOrderAmountBuy = api_url + "order/amount-buy";
_api.ApiOrderDetail = api_url + "order/detail";
_api.ApiCartGetCoupon = api_url + "cart/get-coupon";

/**************** 支付 ***************/
_api.ApiWxPay = api_url + "pay/xcx";
_api.ApiWechatAppPay = api_url + "pay/wapp";
_api.ApiAliPay = api_url + "pay/alipay";

/**************** 会员相关 ***************/
_api.ApiVersion = api_url + "version/index";
_api.ApiAbout = api_url + "user/about";
_api.ApiUserPoint = api_url + "member/pointLog";
_api.ApiUserAddress = api_url + "member/address/index";
_api.ApiUserEditAddress = api_url + "member/address/update";
_api.ApiUserCreateAddress = api_url + "member/address/create";
_api.ApiUserCollect = api_url + "member/collect";
_api.ApiUserInfo = api_url + "member/update";
_api.ApiUserIndex= api_url + "member/info";
_api.ApiUserMessage= api_url + "member/message";
_api.ApiUserOpenId= api_url + "member/openId";
_api.ApiUserMessageRead= api_url + "member/messageRead";
_api.ApiUserCoupon= api_url + "member/coupon";
_api.ApiUserGetInfo = api_url + "user/get-info";
_api.ApiUserRegister = api_url + "user/register";
_api.ApiOrderIndex = api_url + "order/index";
_api.ApiOrderReturn = api_url + "order/return";
_api.ApiOrderCancel = api_url + "order/cancel";
_api.ApiOrderConfirm = api_url + "order/confirm";
_api.ApiUserRegion= api_url + "user/region";
_api.ApiCouponList= api_url + "coupon/list";
_api.ApiCouponReceive= api_url + "coupon/receive";
_api.ApiCouponAllList= api_url + "coupon/all-list";
_api.ApiSellerActiveOff= api_url + "seller/active-off";
_api.ApiUserDistribution= api_url + "user/distribution";
_api.ApiUserStatis= api_url + "user/statis";
_api.ApiUserRank= api_url + "user/rank";
_api.ApiUserDetails= api_url + "user/details";
_api.ApiUserCode= api_url + "user/code";

//_api.ApiUserSignIndex= api_url + "member/sign";
_api.ApiUserSign= api_url + "member/sign";
_api.ApiUserSignLog= api_url + "member/signLog";
_api.ApiUserSignData= api_url + "member/signData";

_api.ApiUserChange= api_url + "user/change";

/**************** 会员登陆模块 ***************/
_api.ApiUserLogin = api_url + "auth/login";
_api.ApiUserPE = api_url + "login/p-e";


/**************** 商家相关 ***************/
/* _api.ApiSeller = api_url + "seller/seller";
_api.ApiInfo = api_url + "seller/info";
_api.ApiOpenShop = api_url + "seller/open-shop";
_api.ApiSellerGoods = api_url + "seller/goods";
_api.ApiSellerDelGoods = api_url + "seller/goods-del";
_api.ApiSellerPullGoods = api_url + "seller/goods-pull";
_api.ApiSellerPushGoods = api_url + "seller/goods-push";
_api.ApiSellerSellerOrder = api_url + "seller/seller-order";
_api.ApiSellerActivety = api_url + "seller/activity";
_api.ApiSellerSellerShipping = api_url + "seller/seller-shipping";          
_api.ApiSellerShowShop = api_url + "seller/show-shop";
_api.ApiSellerWallet = api_url + "seller/wallet";
_api.ApiSellerActivetyGoods = api_url + "seller/activity-goods";
_api.ApiSellerActivetySubgoods = api_url + "seller/activity-subgoods"; */

/******************** 帮助中心 *********************/
_api.ApiConfigArticle = api_url + "config/article";
_api.ApiConfigArticleDetail = api_url + "config/articleDetail";


/******************** 订单相关 *********************/
_api.ApiOrderOrderGoods = api_url + "order/goods";
_api.ApiOrderGoods = api_url + "order/goods";
_api.ApiCommentSub = api_url + "order/comment/add";
_api.ApiReturnApply = api_url + "after/apply";
_api.ApiReturnList = api_url + "after/list";
_api.ApiReturnDetail = api_url + "after/detail";
_api.ApiReturnReview = api_url + "return/review";


/******************** 当前版本号 *********************/
_api.ApiVersionText = "1.0.0"; 
_api.ApiVersionCode = 10; 
//_api.ApiWebSocket = 'ws://116.63.33.51:7272'; 

Vue.config.productionTip = false
Vue.prototype.$fire = new Vue();
Vue.prototype.$store = store;
Vue.prototype.$api = {msg, json, prePage,loading,checkMobile,checkEmail,_api, postUrl, random_char,getUrl,putUrl,deleteUrl,timestampToTime, setCartNumber};

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount() 
